package cn.cupk.logsystem.log.factoryPattern;

import cn.cupk.logsystem.log.observerPattern.Handler;
import cn.cupk.logsystem.log.LogRecord;
import cn.cupk.logsystem.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DatabaseHandler extends Handler {
    @Autowired
    private LogMapper logMapper;

    @Override
    public void publish(LogRecord record) {
        try {
            logMapper.insertLog(record);
        } catch (Exception e) {
            System.err.println("Failed to insert into database: " + e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void flush() {
        // No specific implementation needed
    }

    @Override
    public void close() {
        // No specific implementation needed
    }
}
